Index.addIndex   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
import {ForeignKey} from "../Model";
2
import {IndexInterface, ModelInterface} from "../../JeloquentInterfaces";
3
4
export default class Index implements IndexInterface {
5
6
    private _indexes: Map<string, Map<string, Set<string>>>;
7
8
    private indexedFields: Set<string>;
9
10
    private splitIndexNames: Map<string, string[]>;
11
12
    constructor() {
13
        this._indexes = new Map();
14
        this.indexedFields = new Set();
15
        this.splitIndexNames = new Map();
16
    }
17
18
    get indexes(): Map<string, Map<string|number, Set<string|number>>> {
19
        return this._indexes;
20
    }
21
22
    static add(model: ModelInterface, foreignKeyField: ForeignKey): void {
23
        globalThis.Store.database().addIndex(model.className, foreignKeyField.foreignKey, foreignKeyField.value, model.primaryKey);
24
    }
25
26
    /**
27
     * @deprecated
28
     */
29
    static addIndex(model: ModelInterface, foreignKeyField:ForeignKey): void {
30
        this.add(model, foreignKeyField);
31
    }
32
33
    static register(model: ModelInterface, indexName: string): void {
34
        globalThis.Store.database().registerIndex(model.className, indexName);
35
    }
36
37
    /**
38
     * @deprecated
39
     */
40
    static registerIndex(model: ModelInterface, indexName: string): void {
41
        this.register(model, indexName)
42
    }
43
44
    static remove(model: ModelInterface, foreignKeyField: ForeignKey): void {
45
        globalThis.Store.database().removeIndex(model.className, foreignKeyField.foreignKey, foreignKeyField.previousValue, model.primaryKey);
46
    }
47
48
    /**
49
     * @deprecated
50
     */
51
    static removeIndex(model: ModelInterface, foreignKeyField: ForeignKey): void {
52
        this.remove(model, foreignKeyField);
53
    }
54
55
    static removeTmpIdFromIndex(model: ModelInterface) {
56
        const className = model.className;
57
        model.dirtyFields
58
            .filter(field => field.constructor.name === 'ForeignKey')
59
            .forEach((field) => {
60
                globalThis.Store.database().removeIndex(className, field.name, field.originalValue, model._tmpId);
61
            });
62
    }
63
64
    public addValue(indexName: string, lookUpKey:string|number, id:string|number): void {
65
        if (!this._indexes.has(indexName) || id === null) {
66
            return;
67
        }
68
69
        const index = this.index(indexName);
70
        if (!(index.has(`${lookUpKey}`))) {
71
            this.registerLookUpKey(indexName, lookUpKey, id);
72
            return;
73
        }
74
        const keys = this.indexLookUpKey(indexName, lookUpKey);
75
        if (keys.has(`${id}`)) {
76
            return;
77
        }
78
        keys.add(`${id}`);
79
    }
80
81
    public addValueByModel(model: ModelInterface) {
82
        for (const [indexName] of this._indexes) {
83
            this.addValue(
84
                indexName,
85
                this.getLookUpValue(model, indexName),
86
                model.primaryKey
87
            );
88
        }
89
    }
90
91
    public getIndexByKey(key: string): Map<string, Set<string>> {
92
        return this.index(key);
93
    }
94
95
    public getLookUpValue(model: ModelInterface, fieldName: string): string {
96
        const lookUpValue = this.splitIndexNames.get(fieldName);
97
        let returnValue = null;
98
        let indexLookUpValue = model;
99
        for (const lookUpField of lookUpValue) {
100
            if (indexLookUpValue[`original_${lookUpField}`] === null) {
101
                break;
102
            }
103
            indexLookUpValue = indexLookUpValue[`original_${lookUpField}`];
104
            returnValue = indexLookUpValue;
105
        }
106
107
        return `${returnValue}`;
108
    }
109
110
    public register(indexName: string): void {
111
        if (!this._indexes.has(indexName)) {
112
            this.indexedFields.add(indexName);
113
            this.splitIndexNames.set(indexName, indexName.split('.'));
114
            this._indexes.set(indexName, new Map());
115
        }
116
    }
117
118
    /**
119
     * @deprecated
120
     */
121
    public registerIndex(indexName: string): void {
122
        this.register(indexName);
123
    }
124
125
    public registerLookUpKey(indexName, lookUpKey, id) {
126
        this.index(indexName).set(`${lookUpKey}`, new Set([`${id}`]));
127
    }
128
129
    public removeValue(indexName: string, lookUpKey:string|number, id:string|number): void {
130
        this.indexLookUpKey(indexName, lookUpKey).delete(`${id}`);
131
    }
132
133
    public removeValueByModel(model: ModelInterface): void {
134
        for (const [indexName] of this._indexes) {
135
            this.removeValue(
136
                indexName,
137
                this.getLookUpValue(model, indexName),
138
                model.primaryKey
139
            );
140
        }
141
    }
142
143
    public truncate(): void {
144
        for (const key in this._indexes) {
145
            this.index(key).clear();
146
        }
147
    }
148
149
    public unregisterLookUpKey(indexName:string, lookUpKey:string|number): void {
150
        this.index(indexName).delete(`${lookUpKey}`);
151
    }
152
153
    private index(index: string): Map<string, Set<string>> {
154
        return this._indexes.get(index);
155
    }
156
157
    private indexLookUpKey(index: string, key: string|number): Set<string|number> {
158
        return this.index(index).get(`${key}`);
159
    }
160
}